package com.qlmjx.sevalopos.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.io.IOException;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

public abstract class BaseFragment extends Fragment {

    public View mRootView;

    public final String TAG = getClass().getSimpleName();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = inflater.inflate(getLayoutResId(), container, false);
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) mRootView.getParent();
        if (parent != null) {
            parent.removeView(mRootView);
        }
        initView(mRootView);
        return mRootView;
    }

    /***
     * 获取布局文件
     */
    public abstract int getLayoutResId();

    /***
     * 初始化控件
     */
    public abstract void initView(View view);

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }



    public void refreshAdapter(int position) {

    }

    public void onFragmentRefresh(int position) {

    }

    public void onFragmentRefresh(String str) {

    }


    @Subscribe
    public void onEventMainThread(BaseEvent event) throws IOException {

    }
}
